package com.example.toutiao.ui.main.adpater

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.toutiao.R
import com.example.toutiao.ui.main.model.RecMainModel
import com.example.toutiao.ui.main.model.RecommenModel

class SearchAdapter(model:RecMainModel):RecyclerView.Adapter<SearchAdapter.SearchHolder>() {
    private val model:RecMainModel=model
    private val recommendList:MutableList<RecommenModel>?=model.recommendList
    private val historyList:MutableList<RecommenModel>?=model.historyList
    private val bottomList:MutableList<RecommenModel>?=model.bottomList

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchHolder =SearchHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_search,parent,false))

    override fun onBindViewHolder(holder: SearchHolder, position: Int) {
        holder.tvHsList.layoutManager=GridLayoutManager(holder.itemView.context,2)
        if (model!=null){

            holder.tvLine.isVisible=false
            holder.tvHs.isVisible=false
            when(position){
                0->{
                    if(recommendList!=null&&recommendList.size>0){
                        holder.tvHsList.adapter=SearchChildAdapter(recommendList)
                    }

                }
                1->{
                    if(historyList!=null&&historyList.size>0){
                        holder.tvLine.isVisible=true
                        holder.tvHs.isVisible=true
                        holder.tvHsList.adapter=SearchChildAdapter(historyList)
                    }
                }
                2->{
                    if(bottomList!=null&&bottomList.size>0) {
                        holder.tvLine.isVisible = true
                        holder.tvHsList.adapter = SearchChildAdapter(bottomList)
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int=3

    class SearchHolder(it: View):RecyclerView.ViewHolder(it){
        val tvHs:TextView=it.findViewById(R.id.v_history)
        val tvLine:View=it.findViewById(R.id.v_line)
        val tvHsList:RecyclerView=it.findViewById(R.id.rv_hs_list)
    }
}